package pers.zh.parking.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.EventBusManager;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import pers.zh.parking.R;
import pers.zh.parking.common.Const;
import pers.zh.parking.data.UserSettings;
import pers.zh.parking.data.entity.User;
import pers.zh.parking.di.component.DaggerModifyPwdComponent;
import pers.zh.parking.mvp.contract.ModifyPwdContract;
import pers.zh.parking.mvp.presenter.ModifyPwdPresenter;
import pers.zjc.commonlibs.util.StringUtils;
import pers.zjc.commonlibs.util.ToastUtils;

import static com.jess.arms.utils.Preconditions.checkNotNull;

public class ModifyPwdActivity extends BaseActivity<ModifyPwdPresenter>
        implements ModifyPwdContract.View {

    @BindView(R.id.ivLeft)
    ImageView ivLeft;
    @BindView(R.id.toolbar_left)
    RelativeLayout toolbarLeft;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.etOldPwd)
    EditText etOldPwd;
    @BindView(R.id.etNewPwd)
    EditText etNewPwd;
    @BindView(R.id.etConfirmNewPwd)
    EditText etConfirmNewPwd;
    @BindView(R.id.tvConfirm)
    TextView tvConfirm;
    private UserSettings settings = UserSettings.getInstance();

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerModifyPwdComponent //如找不到该类,请编译一下项目
                                 .builder()
                                 .appComponent(appComponent)
                                 .view(this)
                                 .build()
                                 .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_modify_pwd; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        ivLeft.setOnClickListener(v -> killMyself());
        tvTitle.setText("修改密码");
        tvConfirm.setOnClickListener(v -> {
            String old = etOldPwd.getText().toString();
            String newPwd = etNewPwd.getText().toString();
            String again = etConfirmNewPwd.getText().toString();
            if (!StringUtils.equals(newPwd, again)) {
                showMessage("两次输入新密码不一致");
                return;
            }
            User user = new User();
            user.setAccount(UserSettings.getInstance().getAccount());
            user.setUserId(Integer.valueOf(settings.getUserId()));
            user.setPassword(old);
            user.setNewPwd(newPwd);
            mPresenter.modifyPwd(user);
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ToastUtils.showShort(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void success() {
        EventBusManager.getInstance().post(Const.Action.RE_LOGIN);
        killMyself();
    }
}
